# -*- coding: utf-8 -*-
from imp import reload
import json
from flask import render_template, request, session,redirect,url_for
from .sessions import load_sessions2, load_sessions
from . import hostel
from .models import SchoolCard
import datetime
import sys
from ..models import UserInfo
from sqlalchemy import distinct
reload(sys)
sys.setdefaultencoding('utf8')


@hostel.route('/get_class',  methods=['GET', 'POST'])
def get_class():
    user_id = session.get('user_id')
    if not user_id:
        return redirect(url_for('main.get_login'))
    nowTime = datetime.datetime.now().strftime('%Y-%m-%d')
    if request.args.get('classname'):
        classname = request.args.get('classname')
        majorname = request.args.get('majorname')
        collegename = request.args.get('collegename')
        return render_template('get_class.html', nowTime=nowTime,collegename=collegename,classname=classname,majorname=majorname)
    else:
        return render_template('get_class.html',nowTime=nowTime)


@hostel.route('/showstu',  methods=['GET', 'POST'])
def show_student():
    user_id = session.get('user_id')
    if not user_id:
        return redirect(url_for('main.get_login'))
    classname = request.args.get('classname')
    majorname = request.args.get('majorname')
    collegename = request.args.get('collegename')
    get_time = request.args.get('select_time')
    # print get_time
    sessions = load_sessions2()
    stu_list = sessions.query(SchoolCard).filter(SchoolCard.deptname == classname, SchoolCard.acctime.like("%"+get_time+"%")).order_by(SchoolCard.acctime.desc()).all()
    name_list = sessions.query(SchoolCard.username).filter(SchoolCard.deptname == classname, SchoolCard.acctime.like("%"+get_time+"%")).all()
    sessions.close()
    # 去重
    new_stu_list1 = []
    new_stu_list = []
    c =0
    for _ in stu_list:
        if _.idserial not in new_stu_list1:
            new_stu_list1.append(_.idserial)
            new_stu_list.append(_)
        else:
            pass
        c=c+1
    print c
    sessions = load_sessions()
    stu_list2 = sessions.query(UserInfo.Name).filter(UserInfo.ClassName == classname).all()
    count = len(stu_list2)
    sessions.close()
    final_name = list(set(stu_list2).difference((set(name_list))))
    new_list = []
    for _ in final_name:
        new_list.append(_[0])
    return render_template('show_student.html',classname=classname,stu_list=stu_list,final_name=new_list,get_time=get_time,
                           count=count,majorname=majorname,collegename=collegename,new_stu_list=new_stu_list)

# me
@hostel.route('/me', methods=['GET', 'POST'])
def show_me():
    username = request.args.get('username')
    user_id = request.args.get('userid')
    nowTime = datetime.datetime.now().strftime('%Y-%m-%d')
    sessions = load_sessions2()
    me_list = sessions.query(SchoolCard).filter(SchoolCard.username == username, SchoolCard.idserial == user_id,
                                                SchoolCard.acctime.like("%" + nowTime[:7] + "%")).order_by(SchoolCard.acctime.desc()).all()
    sessions.close()
    return render_template('me.html', me_list=me_list)
